package sunsun.xiaoli.jiarebang.device;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.bean.DeviceListBean;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import com.itboye.pondteam.volley.ResultEntity;
import sunsun.xiaoli.jiarebang.BuildConfig;
import sunsun.xiaoli.jiarebang.R;
import sunsun.xiaoli.jiarebang.app.App;
import sunsun.xiaoli.jiarebang.utils.DeviceType;

import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import static com.itboye.pondteam.utils.EmptyUtil.getSp;

@SuppressLint("NewApi")
public class ManualAddDeviceActivity extends BaseActivity implements Observer {

    App mApp;
    EditText manual_add_device_edit_did;
    EditText mEditTextPassword;
    EditText manual_add_device_edit_nickname;
    Button mButtonDelete;
    Context mContext;
    ManualAddDeviceActivity mThis;
    UserPresenter userPresenter;
    ImageView img_right;
    TextView txt_title, manual_add_device_textView1;
    EditText manual_add_device_edit_password;
    String aq_did;
    private String did;
    private String nickName;
    private String psw;
    ImageView img_back;
    DeviceType deviceType;
    TextView txt_right;
    TextView tv_manual_add, cancelAc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_add_device);
        mApp = (App) getApplication();
        mApp.mManualAddDeviceUi = this;
        manual_add_device_textView1.setText(R.string.shoudong_title2);
        mContext = this;
        deviceType = (DeviceType) getIntent().getSerializableExtra("device");
        aq_did = getIntent().getStringExtra("aq_did");
        mThis = this;
        userPresenter = new UserPresenter(this);
        if (BuildConfig.APP_TYPE.equalsIgnoreCase("pondteam".toLowerCase())) {
            userPresenter.setBaseUrl(Const.pondlink_wrapUrl);
        } else {
            txt_right.setVisibility(View.VISIBLE);
            txt_right.setText(getString(R.string.scan_code));
        }
//        txt_title.setText(getString(R.string.add_device));
        img_right.setVisibility(View.GONE);//setBackgroundResource(R.drawable.ic_action_accept);
        mEditTextPassword = (EditText) findViewById(R.id.manual_add_device_edit_nickname);
        initAddParams(getIntent().getStringExtra("did"), getIntent().getStringExtra("psw"));
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 如果是返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mApp.mManualAddDeviceUi = null;
        }
        return super.onKeyDown(keyCode, event);
    }


    public void addDevice() {
        addDeviceManually();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_right:
                addDeviceManually();
                break;
            case R.id.txt_right:
                //扫描添加
                scan();
                break;
            case R.id.tv_manual_add:
                addDevice();
                break;
            case R.id.cancelAc:
                finish();
                break;
        }
    }

    private void scan() {
//        startActivityForResult(new Intent(this, ScanQRCode.class), 111);
//        startActivityForResult(new Intent(this, CaptureActivity.class),111);
    }

    private void addDeviceManually() {
        did = manual_add_device_edit_did.getText().toString();
        nickName = manual_add_device_edit_nickname.getText().toString();
        if (did.equals("")) {
            MAlert.alert(getString(R.string.deviceid_empty));
            return;
        }
        if (did.length() <= 10) {
            MAlert.alert(getString(R.string.did_error));
            return;
        }
        if (nickName.equals("")) {
            MAlert.alert(getString(R.string.device_name_empty));
            return;
        }
        if (!did.startsWith(deviceType.getDidStart())) {
            MAlert.alert(String.format(getString(R.string.devicetype_error), deviceType.getName()));
            return;
        }
        boolean hasAdd = false;
        //排除已经添加过的设备

        if (aq_did != null && !"".equals(aq_did)) {
            boolean bindYes = hasBindAq();
            if (bindYes) {
                MAlert.alert(getString(R.string.hasBind));
                return;
            }
        } else {
            if (mApp.mDeviceUi == null) {

            } else {
                if (mApp.mDeviceUi.arrayList != null) {
                    for (DeviceListBean deviceListBean : mApp.mDeviceUi.arrayList) {
                        if (deviceListBean.getDid().contains(did)) {
                            hasAdd = true;
                            break;
                        }
                    }
                }
            }
            if (hasAdd) {
                MAlert.alert(getString(R.string.hasAdd));
                return;
            }
        }
        psw = manual_add_device_edit_password.getText().toString();
        if (psw.equals("")) {
            MAlert.alert(getString(R.string.psw_name_empty));
            return;
        }
        HashMap<String, Object> hash = new HashMap<>();
        Gson gson = new Gson();
        showProgressDialog(getStringValue(R.string.requesting), true);
        if (did.startsWith("SCHD")) {
            hash.put("pwd", psw);
            String extra = gson.toJson(hash);
            userPresenter.addDevice(getSp(Const.UID), did, nickName, "chiniao_wifi_camera", extra);
        } else {
            if (BuildConfig.APP_TYPE.equals("pondTeam")) {
                hash.put("notify_email", 1);
            }
            hash.put("first_upd", System.currentTimeMillis() + "");
            hash.put("pwd", psw);
            String extra = gson.toJson(hash);
            String deviceTypeName =  deviceType.getDeviceType();
            if (deviceType.getDeviceType().contains(",")) {
                deviceTypeName = deviceType.getDeviceType().split(",")[0];
            }
            userPresenter.addDevice(getSp(Const.UID), did, nickName, deviceTypeName, extra);
//            switch (deviceType) {
//                case DEVICE_AQ500:
//                    break;
//                case DEVICE_AQ700:
//                    userPresenter.addDevice(getSp(Const.UID), did, nickName, "S03-2", extra);
//                    break;
//                case DEVICE_AQ600:
//                    userPresenter.addDevice(getSp(Const.UID), did, nickName, "S03-3", extra);
//                    break;
//                case DEVICE_AQ640:
//                    userPresenter.addDevice(getSp(Const.UID), did, nickName, "S03-4", extra);
//                    break;
//                case DEVICE_AQ680:
//                    userPresenter.addDevice(getSp(Const.UID), did, nickName, "S03-5", extra);
//                    break;
//                case DEVICE_SHUIZUDENG:
//                    userPresenter.addDevice(getSp(Const.UID), did, nickName, "S06-1", extra);
//                    break;
//                default:
//                    userPresenter.addDevice(getSp(Const.UID), did, nickName, did.substring(0, 3), extra);
//                    break;
//            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 111 && resultCode == RESULT_OK) {
//            if (data != null) {
//                String result = data.getStringExtra(CodeUtils.RESULT_STRING);
//                try {
//                    did = result.substring(0, result.indexOf(","));
//                    psw = result.substring(result.indexOf(",") + 1);
//                    initAddParams(did, psw);
//                } catch (Exception e) {
//
//                }
//            }
//        }
    }

    private void initAddParams(String did, String psw) {
        manual_add_device_edit_did.setText(did);
        manual_add_device_edit_password.setText(psw);
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity entity = handlerError(data);
        closeProgressDialog();
        if (entity != null) {
            if (entity.getCode() != 0) {
                MAlert.alert(entity.getMsg());
                return;
            }
            if (entity.getEventType() == UserPresenter.adddevice_success) {
                MAlert.alert(entity.getData());
                if (mApp.mDeviceUi != null) {
                    mApp.mDeviceUi.getDeviceList();
                }

                if (aq_did != null && !"".equals(aq_did)) {
                    //绑定从设备
                    //判断是否已经在绑定之列
                    boolean bindYes = hasBindAq();
                    if (!bindYes) {
                        userPresenter.cameraBind(getSp(Const.UID), aq_did, did, "chiniao_wifi_camera", did, psw);
                    } else {
                        //结束上一个activity
                        if (mApp.mDeviceUi != null) {
                            mApp.mDeviceUi.mListView.smoothScrollToPosition(0);
                        }
                        //结束当前activity
                        finish();
                    }
                } else {
                    //结束上一个activity

                    if (mApp.addPondDeviceUI != null) {
                        mApp.addPondDeviceUI.finish();
                    }
                  if (mApp.mDeviceUi != null) {
                        mApp.mDeviceUi.mListView.smoothScrollToPosition(0);
                    }
                    //结束当前activity
                    finish();
                }
            } else if (entity.getEventType() == UserPresenter.adddevice_fail) {
                MAlert.alert(entity.getData());
            } else if (entity.getEventType() == UserPresenter.cameraBind_success) {
                //结束上一个activity

                //结束当前activity
                finish();
                MAlert.alert(entity.getData());
            } else if (entity.getEventType() == UserPresenter.cameraBind_fail) {
                MAlert.alert(entity.getData());
            }
        }
    }

    private boolean hasBindAq() {
        boolean isAdd = false;
        return isAdd;
    }

    /**
     * switch (deviceType) {
     *             case DEVICE_AQ806:
     *                 if (!did.startsWith("S03")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng806)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_AQ500:
     *                 if (!did.startsWith("S035")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng500)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_AQ700:
     *                 if (!did.startsWith("S037")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng700)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_AQ640:
     *                 if (!did.startsWith("S0364")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng640)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_AQ680:
     *                 if (!did.startsWith("S0368")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng680)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_AQ600:
     *                 if (!did.startsWith("S036")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng600)));
     *                     return;
     *                 }
     *                 break;
     *
     *             case DEVICE_JIAREBANG:
     *                 if (!did.startsWith("S02")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhinengjiarebang)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_PH:
     *                 if (!did.startsWith("S04")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_yuancheng_ph)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_SHUIBENG:
     *                 if (!did.startsWith("S05")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhinengbianpinshuibeng)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_GUOLVTONG:
     *                 if (!did.startsWith("S01")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_chitangguolv)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_CAMERA:
     *             case DEVICE_CAMERA_NEW:
     *                 if (!did.startsWith("SCHD")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), getIntent().getStringExtra("title")));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_SHUIZUDENG:
     *                 if (!did.startsWith("S06")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_shuizudeng)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_QIBENG:
     *                 if (!did.startsWith("S07")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), BuildConfig.APP_TYPE.equals("小绵羊智能") ? MyApplication.getInstance().getResources().getString(R.string.device_zhinengyangqibeng) : MyApplication.getInstance().getResources().getString(R.string.device_zhinengqibeng)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_AQ118:
     *                 if (!did.startsWith("S08")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_zhineng118)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_WEISHIQI:
     *                 if (!did.startsWith("S09")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_weishiqing)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_PET_FEEDER:
     *                 if (!did.startsWith("S10")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_pet_feeder)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_POND_FEEDER:
     *                 if (!did.startsWith("S11")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_pond_feeder)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_HEW_FISHBOWL:
     *                 if (!did.startsWith("S12")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_hew_fish_bowl)));
     *                     return;
     *                 }
     *                 break;
     *             case DEVICE_PET_DRINK:
     *                 if (!did.startsWith("S14")) {
     *                     MAlert.alert(String.format(getString(R.string.devicetype_error), App.getInstance().getString(R.string.device_pet_drink)));
     *                     return;
     *                 }
     *                 break;
     *         }
     */
}
